# -*- coding: utf-8 -*-

from __future__ import division
from utility import *

class Formation:
  def __init__(self, field, name = ""):
    self.field     = field
    self.name      = name
    self.locations = {}

  def getLocation(self, role): return self.locations[role.symbol]

class Standard(Formation):
  def __init__(self, field, opposite=False):
    Formation.__init__(self, field)
    self.name = "Standard formation"
    h = Helper()
    self.locations["GO"] = h.getStandardLocation("GO", field, opposite)
    self.locations["DR"] = h.getStandardLocation("DR", field, opposite)
    self.locations["FF"] = h.getStandardLocation("FF", field, opposite)
    self.locations["DC"] = h.getStandardLocation("DC", field, opposite)
    self.locations["DL"] = h.getStandardLocation("DL", field, opposite)
    self.locations["WR"] = h.getStandardLocation("WR", field, opposite)
    self.locations["MF"] = h.getStandardLocation("MF", field, opposite)
    self.locations["MD"] = h.getStandardLocation("MD", field, opposite)
    self.locations["WL"] = h.getStandardLocation("WL", field, opposite)
    self.locations["FC"] = h.getStandardLocation("FC", field, opposite)
    self.locations["LL"] = h.getStandardLocation("LL", field, opposite)

  def __str__(self): return self.name

class Attack(Formation):
  def __init__(self, field, opposite=False):
    Formation.__init__(self, field)
    self.name = "Fighting formation"
    h = Helper()
    self.locations["GO"] = h.getAttackLocation("GO", field, opposite)
    self.locations["DR"] = h.getAttackLocation("DR", field, opposite)
    self.locations["FF"] = h.getAttackLocation("FF", field, opposite)
    self.locations["DC"] = h.getAttackLocation("DC", field, opposite)
    self.locations["DL"] = h.getAttackLocation("DL", field, opposite)
    self.locations["WR"] = h.getAttackLocation("WR", field, opposite)
    self.locations["MF"] = h.getAttackLocation("MF", field, opposite)
    self.locations["MD"] = h.getAttackLocation("MD", field, opposite)
    self.locations["WL"] = h.getAttackLocation("WL", field, opposite)
    self.locations["FC"] = h.getAttackLocation("FC", field, opposite)
    self.locations["LL"] = h.getAttackLocation("LL", field, opposite)

class Helper:
  def getStandardLocation(self, role, field, opposite=False):
    r = role
    if hasattr(r, 'symbol'):
      r = role.symbol
    if opposite:
      if r == "GO": return Location(field.length -       field.length / 16,     field.width / 2 )
      if r == "LL": return Location(field.length - 2.6 * field.length / 16, 1.1* field.width / 2 )
      if r == "DR": return Location(field.length - 3.3 * field.length / 16, 3 * field.width / 4)
      if r == "DC": return Location(field.length - 3.3 * field.length / 16, 2 * field.width / 4)
      if r == "DL": return Location(field.length - 3.3 * field.length / 16, 1 * field.width / 4)
      if r == "MF": return Location(field.length - 5.2 * field.length / 16, 2 * field.width / 3)
      if r == "MD": return Location(field.length - 5.2 * field.length / 16, 1 * field.width / 3)
      if r == "WR": return Location(field.length - 5.7 * field.length / 16, 5 * field.width / 6)
      if r == "WL": return Location(field.length - 5.7 * field.length / 16, 1 * field.width / 6)
      if r == "FC": return Location(field.length - 6.5 * field.length / 16, 2.7 * field.width / 5)
      if r == "FF": return Location(field.length - 6.5 * field.length / 16, 1.7 * field.width / 5)
    else:
      if r == "GO": return Location(    field.length   / 16,     field.width / 2 )
      if r == "LL": return Location(2.6 * field.length / 16, 1.1* field.width / 2 )
      if r == "DR": return Location(3.3 * field.length / 16, 3 * field.width / 4)
      if r == "DC": return Location(3.3 * field.length / 16, 2 * field.width / 4)
      if r == "DL": return Location(3.3 * field.length / 16, 1 * field.width / 4)
      if r == "MF": return Location(5.2 * field.length / 16, 2 * field.width / 3)
      if r == "MD": return Location(5.2 * field.length / 16, 1 * field.width / 3)
      if r == "WR": return Location(5.7 * field.length / 16, 5 * field.width / 6)
      if r == "WL": return Location(5.7 * field.length / 16, 1 * field.width / 6)
      if r == "FC": return Location(6.5 * field.length / 16, 2.7 * field.width / 5)
      if r == "FF": return Location(6.5 * field.length / 16, 1.7 * field.width / 5)
    raise Exception("Unexpected role symbol: " + str(r))

  def getAttackLocation(self, role, field, opposite=False):
    r = role
    if hasattr(r, 'symbol'):
      r = role.symbol
    if opposite:
      if r == "GO": return Location(field.length -     field.length   / 16,     field.width / 2 )
      if r == "LL": return Location(field.length - 2.7 * field.length / 16, 1.1 * field.width / 2)
      if r == "DR": return Location(field.length - 3.5 * field.length / 16, 3 * field.width / 4)
      if r == "DC": return Location(field.length - 3.5 * field.length / 16, 2 * field.width / 4)
      if r == "DL": return Location(field.length - 3.5 * field.length / 16, 1 * field.width / 4)
      if r == "MF": return Location(field.length - 5.9 * field.length / 16, 2 * field.width / 3)
      if r == "MD": return Location(field.length - 5.9 * field.length / 16, 1 * field.width / 3)
      if r == "WR": return Location(field.length - 7.5 * field.length / 16, 4.5 * field.width / 6)
      if r == "WL": return Location(field.length - 7.5 * field.length / 16, 1 * field.width / 5)
      if r == "FC": return Location(field.length - 7.95 * field.length / 16,    field.width / 2)
      if r == "FF": return Location(field.length - 7.95 * field.length / 16, 1 * field.width / 3)
    else:
      if r == "GO": return Location(      field.length / 16,     field.width / 2 )
      if r == "LL": return Location(2.7 * field.length / 16, 1.1 * field.width / 2)
      if r == "DR": return Location(3.5 * field.length / 16, 3 * field.width / 4)
      if r == "DC": return Location(3.5 * field.length / 16, 2 * field.width / 4)
      if r == "DL": return Location(3.5 * field.length / 16, 1 * field.width / 4)
      if r == "MF": return Location(5.9 * field.length / 16, 2 * field.width / 3)
      if r == "MD": return Location(5.9 * field.length / 16, 1 * field.width / 3)
      if r == "WR": return Location(7.5 * field.length / 16, 4.5 * field.width / 6)
      if r == "WL": return Location(7.5 * field.length / 16, 1 * field.width / 6)
      if r == "FC": return Location(7.95 * field.length / 16,    field.width / 2)
      if r == "FF": return Location(7.95 * field.length / 16, 1 * field.width / 3)
    raise Exception("Unexpected role symbol: " + str(r))

  def getLineNumber(self, role):
    r = role
    if hasattr(r, 'symbol'):
      r = role.symbol
    if r == "GO": return 4
    if r == "DR": return 3
    if r == "DC": return 3
    if r == "DL": return 3
    if r == "MF": return 2
    if r == "MD": return 2
    if r == "WR": return 1
    if r == "WL": return 1
    if r == "FC": return 1
    if r == "LL": return 0
    if r == "FF": return 0